/* eslint-disable indent */
<template>
<div>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <h1 style=" margin:40px 100px 10px 200px">logo</h1>
      </div>
      <!-- 登入区 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width=" 0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-s-goods" placeholder="密码" type="password"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登入</el-button>
          <el-button type="info" @click="resetLoginForm">重置密码</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</div>
</template>

<script>
// 把json变from
import qs from 'qs'
export default {
  // eslint-disable-next-line space-before-function-paren
  data() {
    return {
      // 绑定数据
      loginForm: {
        username: 'admin',
        password: ''
      },
      // 表单的验证
      loginFormRules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }, {
          min: 3,
          max: 5,
          message: '长度在 3 到 5 个字符',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }, {
          min: 3,
          max: 6,
          message: '长度在 3 到 6 个字符',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    // eslint-disable-next-line space-before-function-paren
    // 重置
    resetLoginForm () {
      // eslint-disable-next-line semi
      this.loginFormRef.resetFields();
      // console.log(this)
    },
    // eslint-disable-next-line space-before-function-paren
    // 登录
    login () {
      this.loginFormRef.validate(async valid => {
        // console.log(qs.stringify(this.loginForm))
        // eslint-disable-next-line semi
        if (!valid) return;
        // eslint-disable-next-line quotes
        const res = await this.$axios.post('http://47.106.237.228:10002/user/login', qs.stringify(this.loginForm))
        const status = res.data.status
        // console.log(status)
        if (status === '2') {
          return this.$message.error('登录失败，请检查密码')
        } else if (status === '1') {
          this.$message.success('登录成功')
          //this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .login_container {
        height: 100%;
        background-color: rgb(131, 122, 122);
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: rgb(204, 198, 198);
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        .avatar_box {
            height: 130px;
            width: 100%;
        }
        .btns {
            display: flex;
            justify-content: flex-end;
        }
        .login_form {
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 0 70px;
            box-sizing: border-box;
        }
    }
</style>
